package com.mvu.banana.common.client;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FocusWidget;
import com.mvu.banana.common.widget.ErrorDialogBox;

/**
 * Class description...
 *
 * @author gantz
 */
public abstract class AsyncCallbackAdapter<T extends ValueDTO> implements AsyncCallback<T> {
  private FocusWidget trigger;

  public AsyncCallbackAdapter() {
  }

  public AsyncCallbackAdapter(FocusWidget trigger) {
    this.trigger = trigger;
  }

  public void onFailure(Throwable throwable) {
    if (trigger != null) {
      trigger.setEnabled(true);
    }
    ErrorDialogBox errorDialogBox = new ErrorDialogBox();
    errorDialogBox.setMessage(throwable.toString());
    errorDialogBox.show();
    throwable.printStackTrace();
  }

  @Override
  public void onSuccess(T t) {
    if (trigger != null) {
      trigger.setEnabled(true);
    }
    afterUpdate(t);
  }

  protected abstract void afterUpdate(T dto);
}
